package com.dspsemi.wodi;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.dspsemi.wodi.adapter.ChatListAdapter;
import com.dspsemi.wodi.adapter.HonorListAdapter;
import com.dspsemi.wodi.adapter.PlayerListAdapter;
import com.dspsemi.wodi.adapter.RoomListAdapter;
import com.dspsemi.wodi.entity.Chat;
import com.dspsemi.wodi.entity.Player;
import com.dspsemi.wodi.entity.Room;
import com.dspsemi.wodi.util.AppConst;

public class HallActivity extends Activity {

	private Button roomBtn;
	private Button playersBtn;
	private Button chatBtn;
	private Button myselfBtn;
	private Button settingsBtn;
	private Button honorBtn;

	private ListView room_listview;
	private ArrayList<Room> room_list;
	private RoomListAdapter room_adapter;

	private ListView player_listview;
	private ArrayList<Player> player_list;
	private PlayerListAdapter player_adapter;

	private ListView chat_listview;
	private ArrayList<Chat> chat_list;
	private ChatListAdapter chat_adapter;

	private ListView honor_listview;
	private ArrayList<Honor> honor_list;
	private HonorListAdapter honor_adapter;

	private RelativeLayout chat_layout;
	private TextView title;
	private Button send;
	private EditText input;

	private Handler mHandler;

	private RelativeLayout info_layout;
	private TextView info;
	private RelativeLayout room_layout;
	private RelativeLayout settings_layout;

	private SeekBar bg_voice;
	private Button logout;

	private Button create_room;
	private Button random_room;
	private Button show_waiting_all;

	private RelativeLayout honor_layout;
	private Button countryBtn;
	private Button provinceBtn;
	private Button cityBtn;
	private RelativeLayout country_layout;
	private RelativeLayout province_layout;
	private RelativeLayout city_layout;

	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hall);

		initViews();
		setListeners();
		initHandler();

		getAllRooms();
	}

	private void getChatMessages() {
		chat_list.clear();

		Chat r = new Chat();
		r.setName("小明");
		r.setContent("谁在房间里呀");
		Chat r1 = new Chat();
		r1.setName("屠洪刚");
		r1.setContent("登山包");
		Chat r2 = new Chat();
		r2.setName("东方不败");
		r2.setContent("任我行在哪里");

		chat_list.add(r);
		chat_list.add(r1);
		chat_list.add(r2);

		chat_adapter.notifyDataSetChanged();

	}

	private void getAllRooms() {
		pd.setMessage("正在获取房间列表");
		pd.show();

		room_list.clear();

		Room r = new Room();
		r.setNumber("001");
		r.setName("8人间标准场");
		r.setPeople("4/8");
		r.setTime("30秒");
		r.setStatus("等待中");

		Room r1 = new Room();
		r1.setNumber("002");
		r1.setName("8人间标准场");
		r1.setPeople("5/8");
		r.setTime("40秒");
		r.setStatus("游戏中");

		Room r2 = new Room();
		r2.setNumber("003");
		r2.setName("8人间标准场");
		r2.setPeople("6/8");
		r.setTime("1分钟");
		r.setStatus("等待中");

		room_list.add(r);
		room_list.add(r1);
		room_list.add(r2);

		room_adapter.notifyDataSetChanged();
		pd.dismiss();
	}

	private void getAllPlayers() {
		player_list.clear();

		Player r = new Player();
		r.setName("小明");
		r.setSheng("500");
		r.setBai("100");
		r.setJifen("10086");
		r.setDengji("52级");

		Player r1 = new Player();
		r1.setName("阿毛");
		r1.setSheng("25");
		r1.setBai("1000");
		r1.setJifen("35");
		r1.setDengji("3级");

		Player r2 = new Player();
		r2.setName("圣斗士");
		r2.setSheng("5000");
		r2.setBai("1");
		r2.setJifen("20555");
		r2.setDengji("99级");

		player_list.add(r);
		player_list.add(r1);
		player_list.add(r2);

		player_adapter.notifyDataSetChanged();
	}

	private void initHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case AppConst.MSG_JOIN_ROOM_SUCCESS:
					pd.dismiss();
					int position = msg.arg1;
					Intent i = new Intent();
					i.setClass(HallActivity.this, RoomActivity.class);
					String people = room_list.get(position).getPeople();
					int index = people.lastIndexOf("/");
					int people_total = Integer.parseInt(people
							.substring(index + 1));
					i.putExtra("people_total", people_total);
					startActivity(i);
					break;

				case AppConst.MSG_JOIN_ROOM_FAIL:
					pd.dismiss();
					toast("加入房间失败，稍后再试");
					break;

				case AppConst.MSG_GET_ALL_ROOMS_SUCCESS:

					break;
				case AppConst.MSG_GET_ALL_ROOMS_FAIL:

					break;
				default:
					break;
				}
			}
		};
	}

	private void initViews() {
		roomBtn = (Button) findViewById(R.id.roomBtn);
		playersBtn = (Button) findViewById(R.id.playersBtn);
		chatBtn = (Button) findViewById(R.id.chatBtn);
		myselfBtn = (Button) findViewById(R.id.myselfBtn);
		settingsBtn = (Button) findViewById(R.id.settingsBtn);
		honorBtn = (Button) findViewById(R.id.honorBtn);

		title = (TextView) findViewById(R.id.title);

		room_listview = (ListView) findViewById(R.id.room_listview);
		room_list = new ArrayList<Room>();
		room_adapter = new RoomListAdapter(this, room_list);
		room_listview.setAdapter(room_adapter);

		player_listview = (ListView) findViewById(R.id.players_listview);
		player_list = new ArrayList<Player>();
		player_adapter = new PlayerListAdapter(this, player_list);
		player_listview.setAdapter(player_adapter);

		chat_listview = (ListView) findViewById(R.id.chat_listview);
		chat_list = new ArrayList<Chat>();
		chat_adapter = new ChatListAdapter(this, chat_list);
		chat_listview.setAdapter(chat_adapter);

		honor_listview = (ListView) findViewById(R.id.honor_listview);
		honor_list = new ArrayList<Honor>();
		honor_adapter = new HonorListAdapter(this, honor_list);
		honor_listview.setAdapter(honor_adapter);

		chat_layout = (RelativeLayout) findViewById(R.id.chat_layout);
		input = (EditText) findViewById(R.id.input);
		send = (Button) findViewById(R.id.send);

		info = (TextView) findViewById(R.id.info);
		info_layout = (RelativeLayout) findViewById(R.id.info_layout);

		room_layout = (RelativeLayout) findViewById(R.id.room_layout);
		settings_layout = (RelativeLayout) findViewById(R.id.settings_layout);

		logout = (Button) findViewById(R.id.logout);
		bg_voice = (SeekBar) findViewById(R.id.bg_voice);

		create_room = (Button) findViewById(R.id.create_room);
		random_room = (Button) findViewById(R.id.random_room);
		show_waiting_all = (Button) findViewById(R.id.show_waiting_all);

		honor_layout = (RelativeLayout) findViewById(R.id.honor_layout);

		countryBtn = (Button) findViewById(R.id.countryBtn);
		provinceBtn = (Button) findViewById(R.id.provinceBtn);
		cityBtn = (Button) findViewById(R.id.cityBtn);

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
	}

	private void setListeners() {
		countryBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getHonor_country();
			}
		});

		provinceBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getHonor_province();
			}
		});

		cityBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getHonor_city();
			}
		});

		create_room.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showCreateRoomDialog();
			}
		});

		random_room.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 随机进入“等待中”并且未满人的房间
			}
		});

		show_waiting_all.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 列表切换 “全部”和“等待中”
			}
		});

		logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences sp = getSharedPreferences("wodi", 0);
				sp.edit().putString("name", "").commit();
				sp.edit().putString("password", "").commit();
				sp.edit().putString("nickname", "").commit();
				sp.edit().putString("jifen", "").commit();
				sp.edit().putString("sheng", "").commit();
				sp.edit().putString("bai", "").commit();
				finish();
			}
		});

		room_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				// 进入该房间
				pd.setMessage("正在申请加入该房间");
				pd.show();
				new Thread() {
					@Override
					public void run() {
						joinOneRoom(position);
					}
				}.start();
			}
		});

		roomBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				room_layout.setVisibility(View.VISIBLE);
				player_listview.setVisibility(View.INVISIBLE);
				chat_layout.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.INVISIBLE);
				settings_layout.setVisibility(View.INVISIBLE);
				honor_layout.setVisibility(View.INVISIBLE);
				title.setText("房间号             房间名              人数         出手时间          状态");
				getAllRooms();
			}
		});

		playersBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				room_layout.setVisibility(View.INVISIBLE);
				player_listview.setVisibility(View.VISIBLE);
				chat_layout.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.INVISIBLE);
				settings_layout.setVisibility(View.INVISIBLE);
				honor_layout.setVisibility(View.INVISIBLE);
				title.setText("昵称             等级                      积分               胜率");
				getAllPlayers();
			}
		});

		chatBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				title.setText("聊天室");
				chat_layout.setVisibility(View.VISIBLE);
				room_layout.setVisibility(View.INVISIBLE);
				player_listview.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.INVISIBLE);
				settings_layout.setVisibility(View.INVISIBLE);
				honor_layout.setVisibility(View.INVISIBLE);
				getChatMessages();
			}
		});

		myselfBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				title.setText("个人信息");
				chat_layout.setVisibility(View.INVISIBLE);
				room_layout.setVisibility(View.INVISIBLE);
				player_listview.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.VISIBLE);
				settings_layout.setVisibility(View.INVISIBLE);
				honor_layout.setVisibility(View.INVISIBLE);

				getMyInfo();
			}
		});

		honorBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				title.setText("荣誉榜单");
				chat_layout.setVisibility(View.INVISIBLE);
				room_layout.setVisibility(View.INVISIBLE);
				player_listview.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.INVISIBLE);
				settings_layout.setVisibility(View.INVISIBLE);
				honor_layout.setVisibility(View.VISIBLE);

				getHonor_country();
			}
		});

		settingsBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				title.setText("设置");
				chat_layout.setVisibility(View.INVISIBLE);
				room_layout.setVisibility(View.INVISIBLE);
				player_listview.setVisibility(View.INVISIBLE);
				info_layout.setVisibility(View.INVISIBLE);
				settings_layout.setVisibility(View.VISIBLE);
				honor_layout.setVisibility(View.INVISIBLE);

			}
		});

		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String inputStr = input.getText().toString();
				if (inputStr == null || inputStr.length() == 0) {
					toast("请输入聊天内容");
					return;
				}

				send(inputStr);
			}
		});

		bg_voice.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// set voice

			}
		});
	}

	private void joinOneRoom(int position) {
		Message msg = new Message();
		msg.what = AppConst.MSG_JOIN_ROOM_SUCCESS;
		msg.arg1 = position;
		mHandler.sendMessage(msg);
	}

	protected void getMyInfo() {
		SharedPreferences sp = getSharedPreferences("wodi", 0);
		String nickname = sp.getString("nickname", "");
		String sheng = sp.getString("sheng", "0");
		String bai = sp.getString("bai", "0");
		String jifen = sp.getString("jifen", "0");

		info.setText("昵称：" + nickname + " 胜:" + sheng + " 败:" + bai + " 积分："
				+ jifen);
	}

	protected void getHonor_city() {
		honor_list.clear();

		Honor h1 = new Honor();
		h1.setName("雷破天");
		h1.setJifen("50000");
		h1.setFrom("无锡");

		Honor h2 = new Honor();
		h2.setName("石惊天");
		h2.setJifen("38880");
		h2.setFrom("徐州");

		Honor h3 = new Honor();
		h3.setName("我是小卧底");
		h3.setJifen("25550");
		h3.setFrom("镇江");

		honor_list.add(h1);
		honor_list.add(h2);
		honor_list.add(h3);

		honor_adapter.notifyDataSetChanged();
	}

	protected void getHonor_province() {

		honor_list.clear();

		Honor h1 = new Honor();
		h1.setName("雷破天");
		h1.setJifen("50000");
		h1.setFrom("江苏无锡");

		Honor h2 = new Honor();
		h2.setName("石惊天");
		h2.setJifen("38880");
		h2.setFrom("广东深圳");

		Honor h3 = new Honor();
		h3.setName("我是小卧底");
		h3.setJifen("25550");
		h3.setFrom("浙江义乌");

		honor_list.add(h1);
		honor_list.add(h2);
		honor_list.add(h3);

		honor_adapter.notifyDataSetChanged();

	}

	protected void getHonor_country() {
		honor_list.clear();

		Honor h1 = new Honor();
		h1.setName("雷破天");
		h1.setJifen("50000");
		h1.setFrom("江苏无锡");

		Honor h2 = new Honor();
		h2.setName("石惊天");
		h2.setJifen("38880");
		h2.setFrom("广东深圳");

		Honor h3 = new Honor();
		h3.setName("我是小卧底");
		h3.setJifen("25550");
		h3.setFrom("浙江义乌");

		honor_list.add(h1);
		honor_list.add(h2);
		honor_list.add(h3);

		honor_adapter.notifyDataSetChanged();
	}

	protected void showCreateRoomDialog() {
		LayoutInflater layoutIn = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutIn.inflate(R.layout.dialog_create_room, null);
		new AlertDialog.Builder(this).setTitle("创建房间")
				.setIcon(android.R.drawable.ic_dialog_info).setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						createNewRoom();
					}
				}).setNegativeButton("取消", null).create().show();
	}

	protected void createNewRoom() {

	}

	protected void send(String inputStr) {
		SharedPreferences sp = getSharedPreferences("wodi", 0);
		String nickname = sp.getString("nickname", "");

		Chat cm = new Chat();

		cm.setName(nickname);
		cm.setContent(inputStr);
		chat_list.add(cm);
		chat_adapter.notifyDataSetChanged();

		chat_listview.setSelection(chat_listview.getBottom());
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
